﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using YLClient.Froms;
using YLClient.YLServiceReference;
using YLClient.GlobalClass;
using YLClient.Util;

namespace YLClient
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        UserServiceClient client;
        DateTime[] currentDates;
        public MainWindow()
        {
            InitializeComponent();
            initializeData();
            //initializeUI();
        }

        private void initializeData()
        {
            client = GlobalClass.DataContainer.Client;
        }
        //private void initializeUI()
        //{
        //    currentDates = DataContainer.Dict["ItemDates"] as DateTime[];
        //    foreach (var date in currentDates)
        //    {
        //        CalendarItemFilter.SelectedDates.Add(date);
        //    }
        //    CalendarItemFilter.SelectedDatesChanged += CalendarItemFilter_SelectedDatesChanged;
        //    CalendarItemFilter.SelectedDate = DateTime.Now;
        //}

        //private void updateCalender()
        //{
        //    currentDates = client.GetUserWorkItemDates(DataContainer.Guid);
        //    CalendarItemFilter.SelectedDates.Clear();
        //    foreach (var date in currentDates)
        //    {
        //        CalendarItemFilter.SelectedDates.Add(date);
        //    }
        //    CalendarItemFilter.SelectedDatesChanged += CalendarItemFilter_SelectedDatesChanged;
        //}

        private void Add_Click(object sender, RoutedEventArgs e)
        {
            NewItem newItem = new NewItem();
            newItem.ShowDialog();
            //updateCalender();
        }

        private IEnumerable<UserControls.Item> buildItemControls(WorkItem[] items)
        {
            foreach (var item in items)
            {
                UserControls.Item i = new UserControls.Item(item);
                yield return i;
            }
        }

        private void GetItems_Click(object sender, RoutedEventArgs e)
        {

        }


        private void getUserAccount_Click(object sender, RoutedEventArgs e)
        {
            string info = "";
            info += "当前余额:" + DataContainer.Account.Money.ToString();
            MessageBox.Show(info, "帐户余额");
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            client.LoginOut(DataContainer.Guid);
        }

        private void setItems(IEnumerable<WorkItem> items)
        {
            lstItems.Items.Clear();
            foreach (var item in items)
            {
                lstItems.Items.Add(new UserControls.Item(item));
            }
            lstItems.Items.Refresh();
        }
        //private void setCurrentSeleted(IEnumerable<DateTime> dates)
        //{
        //    CalendarItemFilter.SelectedDatesChanged -= CalendarItemFilter_SelectedDatesChanged;
        //    foreach (var date in dates)
        //    {
        //        CalendarItemFilter.SelectedDates.Add(date);
        //    }
        //    CalendarItemFilter.SelectedDatesChanged += CalendarItemFilter_SelectedDatesChanged;
        //}

        //private void CalendarItemFilter_SelectedDatesChanged(object sender, SelectionChangedEventArgs e)
        //{
        //    if (CalendarItemFilter.SelectedDates.Count > 0)
        //    {
        //        var start = CalendarItemFilter.SelectedDates.First().getZeroTime();
        //        var end = CalendarItemFilter.SelectedDates.Last().getFinalTime();
        //        var little = start <= end ? start : end;
        //        var big = start <= end ? end : start;
        //        var items = ItemsManager.GetItems(little, big);
        //        setItems(items);
        //        setCurrentSeleted(CalendarItemFilter.SelectedDates.Concat(currentDates));
        //    }
        //}

        private void Search_Click(object sender, RoutedEventArgs e)
        {
            Froms.Searcher searcher = new Searcher();
            searcher.OnResultArrive += new OnResultArriveEventHandler(searcher_OnResultArrive);
            searcher.ShowDialog();

        }
        void searcher_OnResultArrive(WorkItem[] items)
        {
            setItems(items);
        }

        private void Setting_Click(object sender, RoutedEventArgs e)
        {
            Froms.SettingManager sm = new SettingManager();
            sm.ShowDialog();
        }

    }
}
